package com.czy;

import java.io.*;

public class 替换导出表sql的表名 {

    public static void main(String[] args) {
        String source = "D:/wa_public_opinion_data.sql";   // 你的源文件路径
        String target = "D:/wa_public_opinion_data_copy2.sql";   // 替换后的新文件路径
        try {
            replaceAndSave(source, target);
            System.out.println("替换完成，结果已保存到: " + target);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按行读取文件，替换内容后写入新文件
     * @param sourceFilePath 源文件路径
     * @param targetFilePath 目标文件路径
     * @throws IOException
     */
    public static void replaceAndSave(String sourceFilePath, String targetFilePath) throws IOException {
        File sourceFile = new File(sourceFilePath);
        File targetFile = new File(targetFilePath);

        // 确保目标文件所在目录存在
        if (targetFile.getParentFile() != null) {
            targetFile.getParentFile().mkdirs();
        }

        try (BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile))) {

            String line;
            while ((line = reader.readLine()) != null) {
                // 替换字符串
                String replacedLine = line.replace("wa_public_opinion_data", "wa_public_opinion_data_copy2");
                writer.write(replacedLine);
                writer.newLine(); // 保持换行
            }
        }
    }


}
